from flask import render_template, request, redirect, url_for, flash, session
from . import web_bp
from models import get_all_system_configs, get_system_config_by_key, create_system_config, update_system_config, create_operation_log

# 系统配置管理
@web_bp.route('/system/configs')
def system_configs():
    if 'user_id' not in session:
        return redirect('/login')
    
    configs = get_all_system_configs()
    return render_template('system_configs.html', configs=configs)

# 添加系统配置
@web_bp.route('/system/configs/add', methods=['GET', 'POST'])
def add_system_config():
    if 'user_id' not in session:
        return redirect(url_for('web.login'))
    
    if request.method == 'POST':
        key = request.form['key']
        value = request.form['value']
        description = request.form['description']
        category = request.form['category']
        
        try:
            create_system_config(key, value, description, category)
            flash('配置添加成功')
            # 记录操作日志
            create_operation_log(session['user_id'], session['username'], '添加系统配置', 
                                'POST', request.url, request.remote_addr, 
                                request.headers.get('User-Agent'), f'key: {key}', '成功', 0)
        except Exception as e:
            flash(f'配置添加失败: {str(e)}')
            # 记录操作日志
            create_operation_log(session['user_id'], session['username'], '添加系统配置', 
                                'POST', request.url, request.remote_addr, 
                                request.headers.get('User-Agent'), f'key: {key}', f'失败: {str(e)}', 0)
        
        return redirect(url_for('web.system_configs'))
    
    return render_template('add_system_config.html')

# 编辑系统配置
@web_bp.route('/system/configs/edit/<key>', methods=['GET', 'POST'])
def edit_system_config(key):
    if 'user_id' not in session:
        return redirect(url_for('web.login'))
    
    config = get_system_config_by_key(key)
    if not config:
        flash('配置不存在')
        # 记录操作日志
        create_operation_log(session['user_id'], session['username'], '更新系统配置', 
                            'POST', request.url, request.remote_addr, 
                            request.headers.get('User-Agent'), f'key: {key}', '配置不存在', 0)
        return redirect(url_for('web.system_configs'))
    
    if request.method == 'POST':
        value = request.form['value']
        description = request.form['description']
        category = request.form['category']
        
        try:
            update_system_config(key, value, description, category)
            flash('配置更新成功')
            # 记录操作日志
            create_operation_log(session['user_id'], session['username'], '更新系统配置', 
                                'POST', request.url, request.remote_addr, 
                                request.headers.get('User-Agent'), f'key: {key}', '成功', 0)
        except Exception as e:
            flash(f'配置更新失败: {str(e)}')
            # 记录操作日志
            create_operation_log(session['user_id'], session['username'], '更新系统配置', 
                                'POST', request.url, request.remote_addr, 
                                request.headers.get('User-Agent'), f'key: {key}', f'失败: {str(e)}', 0)
        
        return redirect(url_for('web.system_configs'))
    
    return render_template('edit_system_config.html', config=config)